package com.springbootblog.service.serviceImpl.fontend;

import com.fasterxml.jackson.databind.JsonNode;
import com.springbootblog.controller.fontend.IndexController;
import com.springbootblog.dao.FriendLinkDao;
import com.springbootblog.dao.GossipDao;
import com.springbootblog.pojo.Friendlink;
import com.springbootblog.service.fontend.FriendLinkService;
import com.springbootblog.utils.AES;
import com.springbootblog.utils.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FriendLinkServiceImpl implements FriendLinkService
{
    /**
     * 定义一个静态常量日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    /**
     * 自动装载（dao接口注入）
     */
    @Autowired
    private FriendLinkDao friendLinkDao;

    @Value("${spring.AES.key}")
    public String key;
    @Value("${spring.AES.iv}")
    public String iv;

    /**
     * 友情链接列表
     * @return
     */
    @Override
    public Map<String, Object> getFriendLinkList()
    {
        List<Friendlink> friendLinkList = friendLinkDao.getFriendLinkList();
        Map<String,Object> result = new HashMap<>(3);
        result.put("code", 1);
        result.put("friendLinkList", friendLinkList);
        result.put("msg", "操作成功");
        return result;
    }

    /**
     * 申请友联
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> applyFriendLink(Map<String, Object> params) throws Exception {
        String str = (String) params.get("params");
        // 前端AES加密，后端AES解密
        String param = AES.decryptAES(str.toString(),this.key,this.iv);
        // 获取key对应的值
        String friendlink = Function.stringToJson(param,"friendlink");
        String logo = Function.stringToJson(param,"logo");
        String friendname = Function.stringToJson(param,"friendname");
        // 参数判空
        if((friendlink == null || friendlink.isEmpty()) && (logo == null || logo.isEmpty()) && (friendname == null || friendname.isEmpty()))
        {
            Map<String,Object> result = new HashMap<>(2);
            result.put("code", -1);
            result.put("msg", "参数错误！");
            return result;
        }

        // 查当前表格最大sort_no
        Integer sortNo = friendLinkDao.getMaxSortNo();
        sortNo = sortNo + 1;

        Friendlink friendlinkObj = new Friendlink();
        friendlinkObj.setFriendlink(friendlink);
        friendlinkObj.setLogo(logo);
        friendlinkObj.setFriendname(friendname);
        friendlinkObj.setSortNo(sortNo.shortValue());
        // 写入数据库
        Integer res = friendLinkDao.applyFriendLink(friendlinkObj);
        Map<String,Object> result = new HashMap<>(2);
        result.put("code", 1);
        result.put("msg", "申请成功，待审核！");
        return result;
    }
}
